#!/usr/bin/python2
# -*- coding: utf8 -*-
from PyQt4 import QtCore, QtGui, Qt, QtDeclarative
from models.MonthJobModel import MonthJobModel

class YearJobModel(QtCore.QAbstractListModel):
    MONTH_ROLE = 0
    YEAR_ROLE = 1
    MONTH_MODEL_ROLE = 2

    fromDate = QtCore.QDate.currentDate()

    def __init__(self, db, fromDate=QtCore.QDate.currentDate(), parent=None):
        QtCore.QAbstractListModel.__init__(self, parent)
        self.fromDate = fromDate
        self.setRoleNames({self.MONTH_ROLE: 'month', self.YEAR_ROLE: 'year', self.MONTH_MODEL_ROLE: 'monthModel'})
        self._db = db

    def data(self, index, role):
        if not index.isValid: return QtCore.QVariant()
        if role == self.MONTH_ROLE:
            date = self.fromDate.addMonths(index.row())
            return  date.month()
        if role == self.YEAR_ROLE:
            date = self.fromDate.addMonths(index.row())
            return date.year()
        if role == self.MONTH_MODEL_ROLE:
            date = self.fromDate.addMonths(index.row())
            date.setDate(date.year(), date.month(), 1)
            monthJobModel = MonthJobModel(self._db, date, self)
            return QtCore.QVariant(monthJobModel)
        return QtCore.QVariant()

    def rowCount(self, parent=QtCore.QModelIndex()):
        return 12
